import sun.reflect.generics.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—20
 * Time: 9:38
 */
public class BinarySearchTree {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;
    /**
     * @param key:
     * @return 找到了返回地址，没找到返回null
     * @description 查找key是否存在于二叉搜索树中
     */
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if(cur.val < key) {
                cur = cur.right;
            }else if(cur.val > key) {
                cur = cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }

    /**
     * @param key:
     * @return boolean
     * @description 插入
     */
    public boolean insert(int key) {
        //将k变成一个节点（万一树为空，那么插入这个节点，树中就有key的这个节点了）
        TreeNode node = new TreeNode(key);
        if(root == null) {
            root = node;
            return true;
        }
        TreeNode cur = root;
        //parent记录cur的父亲结点
        TreeNode parent = null;
        while(cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                //存在相同的元素不能插入成功
                return false;
            }
        }
        //cur一直走到最后cur为null
        if(parent.val < key) {
            parent.right = node;
        }else {
           parent.left = node;
        }
        return true;
    }
    /**
     * @param key:
     * @return void
     * @description 删除关键字为key的结点
     */
    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                //找到了，开始删除
                removeNode(cur,parent);
                return;
            }
        }
    }
    /**
     * @param cur: 删除结点
     * @param parent:删除结点的父结点
     * @description 进行删除
     */
    private void removeNode(TreeNode cur, TreeNode parent) {
        if(cur.left == null) {
            if(cur == root) {
                root = root.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if (cur == root) {
                root = root.left;
            }else if(cur == cur.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left == target){
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }
    /**
     * @param root:
     * @return void
     * @description 中序遍历
     */
    public void inorder(TreeNode root) {
        if(root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }
}
